package com.mycjj.android.activity;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.YuyueDateStateBean;
import com.mycjj.android.bean.YuyueDateStateBean.YuyueData;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

public class SelectYuyueTimeActivity extends BaseActivity{
	
	private String type;
	private String date;
	
	private TextView day_textview;
	private String carId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_yuyue_time);
		mActManager.pushActivity(YUYUE_KEY, this);
		Intent intent = getIntent();
		if(null != intent){
			type = intent.getStringExtra("type");
			date = intent.getStringExtra("date");
			carId = intent.getStringExtra("carId");
			Log.i("info", "----获取到的日期--"+date+"type="+type+";carId:"+carId);
		}
		initHeader();
		init();
	}
	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;
	private TextView mTv_store_name;
	private ListView lv_select_time;
	private SelectTimeAdapter list_adapter;
	
//	private ImageView mIv_arrow01,mIv_arrow02,mIv_arrow03,mIv_arrow04,mIv_arrow05,mIv_arrow06,mIv_arrow07,mIv_arrow08;
//	private ImageView mIv_status_01,mIv_status_02,mIv_status_03,mIv_status_04,mIv_status_05,mIv_status_06,mIv_status_07,mIv_status_08;
//	private View mRl_time01_item,mRl_time02_item,mRl_time03_item,mRl_time04_item,mRl_time05_item,mRl_time06_item,mRl_time07_item,mRl_time08_item;
	
	
	
	private void initHeader(){
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("选择时间");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mActManager.popActivity(YUYUE_KEY, SelectYuyueTimeActivity.this);
			}
		});
	}
	
	private void init(){
		mTv_store_name = (TextView)findViewById(R.id.tv_store_name);
		day_textview = (TextView)findViewById(R.id.day_textview);
		day_textview.setText(date);
		lv_select_time = (ListView)findViewById(R.id.lv_select_time);
		list_adapter = new SelectTimeAdapter(this);
		lv_select_time.setAdapter(list_adapter);
		
		lv_select_time.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				SelectTimeAdapter adapter = (SelectTimeAdapter)parent.getAdapter();
				YuyueData weekitem = (YuyueData)adapter.getItem(position);
				int used = weekitem.getUsed();
				int total = weekitem.getTotal();
				if(used < total && total != 0){
					Log.i("info", "---ethan--weekitem.time=="+weekitem.getTime());
					Intent mIntent = new Intent(SelectYuyueTimeActivity.this, SelectYuyueAdviserActivity.class);
					mIntent.putExtra("type", type);
					mIntent.putExtra("date", date);
					mIntent.putExtra("time", weekitem.getTime());
					mIntent.putExtra("carId", carId);
					startActivity(mIntent);
				}else if(used == total){
					Toast.makeText(SelectYuyueTimeActivity.this, weekitem.getTime()+"时间段已经预约满了!", Toast.LENGTH_SHORT).show();
				}
				
			}
		});
		String storeid = Preferences.getSharedPreferences(this,"storeId", "");
		getPlanState(storeid,date);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		mTv_store_name.setText(Preferences.getSharedPreferences(mApp, "storeName", "浙江奥同汽车有限公司"));
	}
	
	private void getPlanState(String storeId,String date){
		RequestParams params = new RequestParams();
		params.put("store_id", storeId);
		params.put("day", date);
		Log.e("mark", "storeId:"+storeId+";day:"+date);
		params.put("client_id",ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		request = new ProjectHttpRequestImpl(this, true);
		request.requestStoreGetDayPlanWithDoneHandler("", params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				Log.i("info", "----ethan==="+content);
				YuyueDateStateBean bean = dataManager.getYuyueDateStateBean();
				if(bean != null){
					List<YuyueData> dataList = bean.getData();
					list_adapter.setList(dataList);
				}
			}
		});
	}
	
	
	
	
	public class SelectTimeAdapter extends TAdapter<YuyueData>{

		public SelectTimeAdapter(Context mContext) {
			super(mContext);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_yuyue_select_time, null);
				holder = new ViewHolder();
				holder.iv_status_01 = (ImageView)convertView.findViewById(R.id.iv_status_01);
				holder.tv_time01 = (TextView) convertView.findViewById(R.id.tv_time01);
				
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			YuyueData yuyueData = mList.get(position);
			int used = yuyueData.getUsed();
			int total = yuyueData.getTotal();
			if(used < total && total != 0){
				holder.iv_status_01.setImageResource(R.drawable.iv_status_free);
			}else if(used == total){
				holder.iv_status_01.setImageResource(R.drawable.iv_status_busy);
			}
			holder.tv_time01.setText(yuyueData.getTime());
			return convertView;
		}
		
		class ViewHolder{
			ImageView iv_status_01;
			TextView tv_time01;
		}
		
	}
	
	private OnClickListener timeOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
		}
	};
}
